package com.ts.service.achievement.impl;

import com.ts.constant.AchievementConstant;
import com.ts.constant.AchievementStatusConstant;
import com.ts.dto.AcademicConferenceDTO;
import com.ts.entity.AcademicConference;
import com.ts.entity.Achievement;
import com.ts.mapper.achievement.AcademicConferenceMapper;
import com.ts.mapper.achievement.AchievementMapper;
import com.ts.service.achievement.AcademicConferenceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * ACADEMIC_CONFERENCE = "academicConference";  //学术会议
 */
@Service
public class AcademicConferenceServiceImpl implements AcademicConferenceService {

    @Autowired
    private AcademicConferenceMapper academicConferenceMapper;

    @Autowired
    private AchievementMapper achievementMapper;

    /**
     * 提交一个学术会议成果
     */
    @Transactional
    @Override
    public void save(AcademicConferenceDTO academicConferenceDTO) {
        // 1.实体类对象拷贝
        AcademicConference academicConference = new AcademicConference();
        BeanUtils.copyProperties(academicConferenceDTO, academicConference);

        // 2.手动添加学术会议公共字段
        academicConference.setCreateTime(LocalDateTime.now());
        academicConference.setUpdateTime(LocalDateTime.now());
        academicConference.setCreateBy(1L);
        academicConference.setUpdateBy(1L);

        // 3.调用Mapper接口
        academicConferenceMapper.save(academicConference);

        //4.成就表数据准备
        Achievement achievement = new Achievement();
        achievement.setName(academicConference.getConferenceName()); //成就名称
        achievement.setType(AchievementConstant.ACADEMIC_CONFERENCE);//成就类型
        achievement.setStatus(AchievementStatusConstant.DRAFT);      //状态
        achievement.setDescription("xxx");                           //描述（预留，需求未确定）
        achievement.setLevel(1);                                     //级别（预留，需求未确定）
        achievement.setDetailId(academicConference.getId());         //外键id
        achievement.setCreatedTime(LocalDateTime.now());             //公共字段
        achievement.setUpdatedTime(LocalDateTime.now());
        achievement.setCreatedBy(1L);
        achievement.setUpdatedBy(1L);

        //调用mapper
        achievementMapper.insert(achievement);

    }

    /**
     * 根据ID查询学术会议详情
     */
    @Override
    public AcademicConferenceDTO getById(Long id) {
        AcademicConference academicConference = academicConferenceMapper.getById(id);
        if (academicConference == null) {
            return null;
        }

        AcademicConferenceDTO dto = new AcademicConferenceDTO();
        BeanUtils.copyProperties(academicConference, dto);
        return dto;
    }

    /**
     * 更新学术会议
     */
    @Transactional
    @Override
    public void update(AcademicConferenceDTO academicConferenceDTO) {
        AcademicConference academicConference = new AcademicConference();
        BeanUtils.copyProperties(academicConferenceDTO, academicConference);
        academicConference.setUpdateTime(LocalDateTime.now());
        academicConference.setUpdateBy(1L);

        academicConferenceMapper.update(academicConference);

        //4.成就表数据准备
        Achievement achievement = new Achievement();
        achievement.setName(academicConference.getConferenceName()); //成就名称
        achievement.setType(AchievementConstant.ACADEMIC_CONFERENCE);//成就类型
        achievement.setStatus(AchievementStatusConstant.DRAFT);      //状态
        achievement.setDescription("xxx");                           //描述（预留，需求未确定）
        achievement.setLevel(1);                                     //级别（预留，需求未确定）
        achievement.setDetailId(academicConference.getId());         //外键id

        achievementMapper.update(achievement);
    }

    /**
     * 根据ID删除学术会议
     */
    @Transactional
    @Override
    public void delete(Long id) {

        //删除成就表
        achievementMapper.deleteById(id);

        academicConferenceMapper.delete(id);
    }
}